package com.chii.order.controller;

import com.chii.order.client.UserClient;
import com.chii.order.po.Order;
import com.chii.order.po.ResponeInfo;
import com.chii.order.po.User;
import com.chii.order.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@Api(value = "OrderController", tags = {"订单操作"})
@RestController
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private UserClient userClient;

    @ApiOperation(value = "创建订单")
    @RequestMapping(value = "/order", method = RequestMethod.POST)
    public ResponeInfo<Order> post(@ApiParam(name = "username", value = "用户名") @RequestParam("username") String username) {
        ResponeInfo<Order> responeInfo = new ResponeInfo<>();
        ResponeInfo<User> userResponeInfo = userClient.getUserByUsernameFromUser(username);
        User user = userResponeInfo.getData();
        Order order = orderService.createOrder(user.getId(),"color");
        responeInfo.setData(order);
        return responeInfo;
    }

    @ApiOperation(value = "获取订单")
    @RequestMapping(value = "/order", method = RequestMethod.GET)
    public ResponeInfo<List<Order>> get(@ApiParam(name = "username", value = "用户名") @RequestParam("username") String username) {
        ResponeInfo<List<Order>> responeInfo = new ResponeInfo<>();
        ResponeInfo<User> userResponeInfo = userClient.getUserByUsernameFromUser(username);
        User user = userResponeInfo.getData();
        List<Order> order = orderService.getOrderByUserId(user.getId());
        responeInfo.setData(order);
        return responeInfo;
    }
}
